package com.xiyuan;

import com.alibaba.fastjson2.JSONObject;
import com.xiyuan.bean.Parameters;
import com.xiyuan.bean.PathBean;
import com.xiyuan.bean.Request;
import com.xiyuan.bean.Schema;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Swagger2PathBuilder extends AbstractPathBuilder {


    public void createJson() {
        LinkedHashMap<String, Request> pathMap = result.getPathMap();
        String parentPath = result.getRoutePath();
        String tag = result.getTags();

        LinkedHashMap result = new LinkedHashMap();
        result.put("openapi", "3.0.1");
        result.put("info", info());
        result.put("tags", tags(tag));
        result.put("paths", paths(pathMap, parentPath, tag));

        JSONObject jsonObject = new JSONObject(result);
        this.result.setJsonString(jsonObject.toString());
    }

    public Map<String, Object> paths(LinkedHashMap<String, Request> pathMap, String parentPath, String tag) {

        LinkedHashMap<String, Object> result = new LinkedHashMap<>();
        pathMap.forEach((key, value) -> {
            LinkedHashMap<String, Object> temp = new LinkedHashMap<>();
            PathBean pathBean = new PathBean();
            temp.put(value.getType(), pathBean);

            pathBean.setSummary(key)
                    .setDeprecated(false)
                    .setDescription("")
                    .setParameters(parameters())
                    .addTags(tag) // 添加标签
                    .setRequestBody(requestBody())
                    .setResponses(responses());
            result.put(parentPath + value.getPath(), temp);
        });
        return result;
    }

    private Map<String, Object> responses() {
        Map<String, Object> result = new LinkedHashMap<>();
        LinkedHashMap<String, Object> value = new LinkedHashMap<>();
        result.put("200", value);

        value.put("description", "成功");
        LinkedHashMap<Object, Object> value1 = new LinkedHashMap<>();
        value.put("content", value1);

        Schema value2 = new Schema("object", null);
        LinkedHashMap<Object, Object> value3 = new LinkedHashMap<>();
        value1.put("application/json", value3);

        value3.put("schema", value2);

        return result;

    }

    private Map<String, Object> requestBody() {
        Map<String, Object> result = new LinkedHashMap<>();
        LinkedHashMap<Object, Object> requestType = new LinkedHashMap<>();
        result.put("content", requestType);

        LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
        Schema schema = new Schema("object", properties);

        LinkedHashMap<Object, Object> value = new LinkedHashMap<>();
        requestType.put("application/json", value);
        value.put("schema", schema);
        return result;
    }

    /**
     * 设置全局参数
     *
     * @return
     */
    public List<Parameters> parameters() {
        List<Parameters> result = new ArrayList<>();
        Parameters build = Parameters.builder()
                .name("token")
                .in("header")
                .description("token身份认证")
                .example("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJjZGNJZCIsImlhdCI6MTY5MTczMzc5OSwic3ViIjoie1widXNlclBvc2lJZFwiOlwiMVwiLFwiYWNjb3VudFwiOlwiYWRtaW5cIn0iLCJleHAiOjE2OTE3NDgxOTl9.j_88GCaCUz8TQ8eNpv_mPoX9tP2ylkrbbh-zDcSgUws")
                .schema(new Schema("string", null))
                .build();
        result.add(build);
        return result;
    }

    public List<Map<String, String>> tags(String tag) {
        List<Map<String, String>> list = new ArrayList<>();
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("name", tag);
        list.add(map);
        return list;
    }

    public Map<String, Object> info() {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("title", "");
        map.put("description", "");
        map.put("version", "");
        return map;
    }
}
